﻿@model RescanWalletModel
@{
    var walletId = Context.GetRouteValue("walletId")!.ToString();
    var w = WalletId.Parse(walletId!);
    ViewData.SetLayoutModel(new LayoutModel($"{nameof(WalletsNavPages.Settings)}-{w.CryptoCode}", StringLocalizer["Rescan Wallet"])
        .SetCategory(WellKnownCategories.ForWallet(w.CryptoCode)));
}

<h3 class="mb-3">@ViewData["Title"]</h3>
@if (!Model.Ok)
{
    <div class="row">
        <div class="col-lg-8">
            <p text-translate="true">This feature is disabled</p>
            @if (Model.IsFullySync)
            {
                <p>
                    <vc:icon symbol="checkmark" css-class="text-success" />
                    <span text-translate="true">The full node is synched</span>
                </p>
            }
            else
            {
                <p>
                    <vc:icon symbol="cross" css-class="text-danger" />
                    <span text-translate="true">The full node is not synched</span>
                </p>
            }
            @if (Model.IsServerAdmin)
            {
                <p>
                    <vc:icon symbol="checkmark" css-class="text-success" />
                    <span text-translate="true">You are server administrator</span>
                </p>
            }
            else
            {
                <p>
                    <vc:icon symbol="cross" css-class="text-danger" />
                    <span text-translate="true">You are not server administrator</span>
                </p>
            }
            @if (Model.IsSupportedByCurrency)
            {
                <p>
                    <vc:icon symbol="checkmark" css-class="text-success" />
                    <span text-translate="true">This full node support rescan of the UTXO set</span>
                </p>
            }
            else
            {
                <p>
                    <vc:icon symbol="cross" css-class="text-danger" />
                    <span text-translate="true">This full node does not support rescan of the UTXO set</span>
                </p>
            }
        </div>
    </div>
}
else if (!Model.Progress.HasValue)
{
    <div class="row">
        @if (Model.PreviousError != null)
        {
            <div class="alert alert-danger alert-dismissible" role="alert">
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="@StringLocalizer["Close"]">
                    <vc:icon symbol="close" />
                </button>
                <span text-translate="true">The previous scan stopped with an error:</span> @Model.PreviousError
            </div>
        }
        else if (Model.LastSuccess != null)
        {
            <div class="alert alert-success alert-dismissible" role="alert">
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="@StringLocalizer["Close"]">
                    <vc:icon symbol="close" />
                </button>
                <span>@ViewLocalizer["The previous scan completed and found <b>{0}</b> UTXOs in <b>{1}</b> (The total UTXO set size is {2})", Model.LastSuccess.Found, Model.TimeOfScan, Model.LastSuccess.TotalSizeOfUTXOSet.Value]</span>
            </div>
        }
        <div class="col-lg-8">
            <p html-translate="true">Scanning the UTXO set allow you to restore the balance of your wallet, but not all the transaction history. This operation will scan the HD Path <b>0/*</b>, <b>1/*</b> and <b>*</b> from a starting index, until no UTXO are found in a whole gap limit.</p>
            <p text-translate="true">The batch size make sure the scan do not consume too much RAM at once by rescanning several time with smaller subset of addresses.</p>
            <p text-translate="true">If you do not understand the above, go with the defaults and start scanning.</p>
        </div>
    </div>

    <div class="row">
        <div class="col-lg-8">
            <form method="post">
                <div class="form-group">
                    <label asp-for="StartingIndex" class="form-label"></label>
                    <input asp-for="StartingIndex" class="form-control" type="number" style="max-width:14ch;" />
                </div>
                <div class="form-group">
                    <label asp-for="GapLimit" class="form-label"></label>
                    <input asp-for="GapLimit" class="form-control" type="number" step="1000" style="max-width:14ch;" />
                </div>
                <div class="form-group">
                    <label asp-for="BatchSize" class="form-label"></label>
                    <input asp-for="BatchSize" class="form-control" type="number" step="1000" style="max-width:14ch;" />
                </div>
                <button type="submit" class="btn btn-primary mt-2" text-translate="true">Start scan</button>
            </form>
        </div>
    </div>
}
else
{
    <div class="row">
        <div class="col-lg-8">
            <p>Scanning in progress, refresh the page to see the progress... (Estimated time: @Model.RemainingTime)</p>
            <div class="progress">
                <div class="progress-bar" role="progressbar" aria-valuenow="@(Model.Progress.Value)"
                     aria-valuemin="0" aria-valuemax="100" style="white-space: nowrap; overflow: hidden; text-overflow: ellipsis; width:@(Model.Progress.Value)%;">
                    @(Model.Progress.Value)% (estimated time: @Model.RemainingTime)
                </div>
            </div>
        </div>
    </div>
}
